var download_chapterlist = function(chapterlist,story_id){
	//var db = openDatabase('mydb_1x2jzv7', '1.0', 'story_1x2jzv7', 25 * 1024 * 1024);
	var chapter_list_table = 'chapter_list'+story_id;
	//alert(chapter_list_table);
	db.transaction(function (tx) {
	//$sql=" SELECT 	story_chapter_id, story_chapter_name FROM chapters_list WHERE story_id = $story_id";
		tx.executeSql('DROP TABLE IF EXISTS '+chapter_list_table);
		items = JSON.parse(chapterlist);
							var index = 0;
							$.each(items,function(index){
								var chapter_index = items[index]['story_chapter_id'];
								var chapter_name = items[index]['story_chapter_name'];
								sql = 	'CREATE TABLE IF NOT EXISTS '+chapter_list_table+' (id INTEGER, chapter_index INTEGER, chapter_name LONGTEXT)';//,'+
								tx.executeSql(sql);
								console.log(sql);
							});
	});
};

var add_db =function(index,content_arr, table_name,success){
//alert (index+' - '+content_arr.length+' - '+content_arr[index].story_chapter_name+' - '+table_name);
	var arr_len = content_arr.length;
	if (index < arr_len&&content_arr[index]['chapter_id']!=undefined){
		//var myVariable = (true condition) ? "true" : "false"
		var chapterindex = content_arr[index]['chapter_id']? content_arr[index]['chapter_id'] : 1;
		var chaptername = content_arr[index]['chapter_name']? content_arr[index]['chapter_name'] : '';
		var chaptercontent = content_arr[index]['chapter_content']? content_arr[index]['chapter_content'] : '';
		db.transaction(function (tx) {
			var sql1 = 'INSERT INTO '+table_name+' (chapter_index, chapter_name, chapter_content) VALUES (?,?,?)';
			tx.executeSql(
				sql1,
				[chapterindex,chaptername,chaptercontent],
				add_db(index,content_arr,table_name), 			//success
				function(e){
					console.log(e);		 						//fail
				}
			);
			console.log(sql1);
		}
		);	
		index++;
		if (index>=arr_len){ remove_ajax_img_loading(800); }
	} 
	/*
	else { 
		return false;
	}
	*/
}
var downloadstory = function(){
	ajax_img_loading('../template/img/ajax-loading.gif');
	console.log('ajax_img_loading in download story');
	//alert('current st id '+localStorage.save_current_story_id +' == '+api_link+'/download_story_content.php');
	$.ajax({
		//url:'../'+api_link+'/download_story_content.php',
		url:api_link+'/chapterlist.download.story.php',
		type:"GET",
		data:{story_id:localStorage.save_current_story_id},
		success:function(data){
			//alert(localStorage.save_current_story_id+data);
			var story_content = JSON.parse(data);
			console.log(story_content);
			db.transaction(function (tx) {
				var story_number = 'story_number_'+localStorage.save_current_story_id;
				tx.executeSql('DROP TABLE IF EXISTS '+story_number);
				sql = 	'CREATE TABLE IF NOT EXISTS '+story_number+' (chapter_index INTEGER UNIQUE, chapter_name VARCHAR(200), chapter_content LONGTEXT)';
				tx.executeSql(sql);
				console.log(sql);
				var index = 0;
				console.log(story_content);
				add_db(index,story_content,story_number);
			});
		},
		error: function(e){
			console.log('e '+e);
		}
	
	});	
};

$('#btn_story_download').click(function(){
	downloadstory();
});

